<template>
  <el-container>
    <el-card shadow="always" class="cardBlock">
      <el-form ref="form" :model="form" label-width="150px" :rules="rules" style="height:auto;margin:50px auto">
        <el-form-item label="套餐类型" prop="mealType">
          <el-radio-group v-model="form.mealType" @change="changeMealType" :disabled="disabled">
            <el-radio v-for="(item,index) in radioList" :key="index" :label="item.value">{{ item.label }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="套餐名称" prop="mealName">
          <el-input v-model="form.mealName" style="width: 300px;"></el-input>
        </el-form-item>
        <el-form-item label="有效期(月)" prop="terOfValidity" v-if="showFormItemMonth">
          <el-input v-model="form.terOfValidity" maxlength="2" style="width: 300px;">
            <template slot="append">月</template>
          </el-input>
        </el-form-item>
        <el-form-item label="有效期(天)" prop="terOfValidity2" v-if="headName!=='LIMIT_SPEED_DAY_PLUS'&&showFormItemDay">
          <el-input v-model="form.terOfValidity2" maxlength="3" style="width: 300px;">
            <template slot="append">天</template>
          </el-input>
        </el-form-item>
        <el-form-item label="有效期(天)" prop="terOfValidity4" v-if="headName==='LIMIT_SPEED_DAY_PLUS'&&form.mealType==='1'">
          <el-input v-model="form.terOfValidity4" maxlength="3" style="width: 300px;">
            <template slot="append">天</template>
          </el-input>
        </el-form-item>
        <el-form-item label="有效期" prop="terOfValidity3" v-if="headName==='TONGYONG_HEAD'">
          <el-input placeholder="有效期" v-model="form.terOfValidity3" class="input-with-select" style="width: 300px;">
            <el-select style="width: 80px" v-model="form.dictTerOfValidity" slot="append" placeholder="单位">
              <el-option label="月" value="MONTH"></el-option>
              <el-option label="天" value="DAY"></el-option>
            </el-select>
            <el-button></el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="流量" prop="mealSpec" v-if="this.form.mealType!=='3'">
          <el-input placeholder="流量" v-model="form.mealSpec" maxlength="7" class="input-with-select" style="width: 300px;">
            <el-select style="width: 80px" v-model="form.mealSpecUnit" slot="append" placeholder="单位">
              <el-option label="MB" value="MB"></el-option>
              <el-option label="GB" value="GB"></el-option>
            </el-select>
            <el-button></el-button>
          </el-input>
        </el-form-item>
        <el-form-item :label="'解封流量阈值('+form.mealSpecUnit+')'" prop="mealSpec6" v-if="this.form.mealType==='3'">
          <el-input v-model="form.mealSpec6" maxlength="7" style="width: 300px;">
            <el-select style="width: 80px" v-model="form.mealSpecUnit" slot="append" placeholder="单位">
              <el-option label="MB" value="MB"></el-option>
              <el-option label="GB" value="GB"></el-option>
            </el-select>
            <el-button></el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="类型" v-if="showFormItemSpeedList">
          <el-radio-group v-model="form.dictOperator" size="small" @change="changCardFact">
            <el-radio label="CHINAMOBILE" border>移动</el-radio>
            <el-radio label="TELECOM" border>电信DCP</el-radio>
            <el-radio label="CMPTELECOM" border>电信CMP</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="限速条件" v-if="showFormItemSpeedList">
          <el-button type="primary" @click="addspeed" icon="el-icon-plus">添加</el-button>
        </el-form-item>
        <!--        //v-if="(headName==='LIMIT_SPEED_DAY'||headName==='LIMIT_SPEED'||headName==='LIMIT_SPEED_DAY_PLUS')&&form.mealType==='1'"-->
        <div v-for="(speed, index) in form.speeds" :key="speed.key" v-if="showFormItemSpeedList">
          <el-row>
            <el-col :span="8">
              <el-form-item
                :label="'限速阈值' + index"
                :prop="'speeds.' + index + '.limitMb'"
                :rules="{
                  required: true, message: '阈值不能为空', trigger: 'blur'
                }"
              >
                <el-input v-model="speed.limitMb" style="width: 170px">
                  <template slot="append" v-if="form.mealSpecUnit==='MB'">MB</template>
                  <template slot="append" v-if="form.mealSpecUnit==='GB'">GB</template>
                </el-input>
              </el-form-item>
            </el-col>

            <el-col :span="10" v-if="form.dictOperator==='TELECOM'">
              <el-form-item
                :label="'限速等级' + index"
                :prop="'speeds.' + index + '.limitSpeed'"
                :rules="{
                  required: true, message: '等级不能为空', trigger: 'blur'
                }"
                v-if="form.dictOperator==='TELECOM'"
              >
                <el-select v-model="speed.limitSpeed" placeholder="请选择" style="width: 120px">
                  <el-option
                    v-for="item in 15"
                    :key="item"
                    :label="item"
                    :value="item">
                  </el-option>
                </el-select>
              </el-form-item>

            </el-col>

            <el-col :span="10" v-if="form.dictOperator==='CHINAMOBILE'">
              <el-form-item
                :label="'限速等级' + index"
                :prop="'speeds.' + index + '.limitSpeed'"
                :rules="{
                  required: true, message: '等级不能为空', trigger: 'blur'
                }"
                v-if="form.dictOperator==='CHINAMOBILE'"
              >
                <el-select v-model="speed.limitSpeed" placeholder="请选择" style="width: 200px">
                  <el-option label="91:APN-AMBR=2Mbps" value="91:APN-AMBR=2Mbps"></el-option>
                  <el-option label="92:APN-AMBR=1Mbps" value="92:APN-AMBR=1Mbps"></el-option>
                  <el-option label="93:APN-AMBR=512Kbps" value="93:APN-AMBR=512Kbps"></el-option>
                  <el-option label="94:APN-AMBR=128Kbps" value="94:APN-AMBR=128Kbps"></el-option>
                </el-select>
              </el-form-item>

            </el-col>
            <el-col :span="10" v-if="form.dictOperator==='CMPTELECOM'">
              <el-form-item
                :label="'限速等级' + index"
                :prop="'speeds.' + index + '.limitSpeed'"
                :rules="{
                  required: true, message: '等级不能为空', trigger: 'blur'
                }"
                v-if="form.dictOperator==='CMPTELECOM'"
              >
                <el-select v-model="speed.limitSpeed" placeholder="请选择" style="width: 200px">
                  <el-option label="1Mbps" value="12"></el-option>
                  <el-option label="3Mbps" value="13"></el-option>
                  <el-option label="5Mbps" value="14"></el-option>
                  <el-option label="10Mbps" value="16"></el-option>
                  <el-option label="20Mbps" value="17"></el-option>
                  <el-option label="不限制" value="31"></el-option>
                </el-select>
              </el-form-item>

            </el-col>
            <el-col :span="4">
              <el-button type="danger" plain @click.prevent="removespeed(speed)">删除</el-button>
            </el-col>

          </el-row>
        </div>
        <el-form-item label="官方售价(元)" prop="officialPrice">
          <el-input v-model="form.officialPrice" maxlength="7" style="width: 300px">
            <template slot="append">元</template>
          </el-input>
        </el-form-item>
        <el-form-item label="代理售价上限(元)" prop="ceilingPrice">
          <el-input v-model="form.ceilingPrice" maxlength="7" style="width: 300px">
            <template slot="append">元</template>
          </el-input>
        </el-form-item>
        <el-form-item label="OEM成本价" prop="oemCostPrice">
          <el-input v-model="form.oemCostPrice" maxlength="7" style="width: 300px">
            <template slot="append">元</template>
          </el-input>
        </el-form-item>
        <el-form-item label="权限值" prop="cardFlowImaginary" v-if="headName!=='TONGYONG_HEAD'">
          <el-input-number v-model="form.cardFlowImaginary" :precision="2" :step="0.1" :max="999" style="width: 200px;"></el-input-number>
        </el-form-item>
        <el-form-item label="上游套餐编号" prop="thirdId" v-if="headName==='TONGYONG_HEAD'">
          <el-input v-model="form.thirdId" style="width: 300px;"></el-input>
        </el-form-item>
        <el-form-item label="排序" prop="seq">
          <el-input v-model="form.seq" maxlength="4" style="width: 300px;"></el-input>
        </el-form-item>
        <el-form-item label="是否分期到账" prop="dictDayPayment" v-if="headName==='LIMIT_SPEED_DAY_PLUS'&&form.mealType==='1'">
          <el-radio-group v-model="form.dictDayPayment" @change="changeDictDayPayment">
            <el-radio label="YES">是</el-radio>
            <el-radio label="NO">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="到账周期" prop="dictDayNumber" v-if="form.dictDayPayment=='YES'&&form.mealType=='1'">
          <el-input v-model="form.dictDayNumber" maxlength="2" placeholder="请填写到账周期数" style="width: 300px">
            <template slot="append">期</template>
          </el-input>
        </el-form-item>
        <el-form-item label="是否分月到账" prop="dictMonthlyPayment" v-if="showFormItemMonth||headName==='TONGYONG_HEAD'">
          <el-radio-group v-model="form.dictMonthlyPayment" @change="changeDictMonthlyPayment">
            <el-radio label="YES">是</el-radio>
            <el-radio label="NO">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否次月生效" prop="dictIsPreStorage" v-if="showFormItemMonth||headName==='TONGYONG_HEAD'||(headName==='LIMIT_SPEED_DAY_PLUS'&&form.mealType==='1')">
          <el-radio-group v-model="form.dictIsPreStorage" @change="changeDictIsPreStorage">
            <el-radio label="YES">是</el-radio>
            <el-radio label="NO">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否连续充值" prop="dictIsTerOfValiditySuper" v-if="showFormItemMonth||headName==='TONGYONG_HEAD'||(headName==='LIMIT_SPEED_DAY_PLUS'&&form.mealType==='1')">
          <el-radio-group v-model="form.dictIsTerOfValiditySuper">
            <el-radio label="C_YES">是</el-radio>
            <el-radio label="C_NO">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item>
          <el-button @click="cancel">取消</el-button>
          <!--v-preventReClick :loading="loading" -->
          <el-button type="primary" @click="onSubmit" v-preventReClick>确定</el-button>
          <el-row style="color:red">注：更改任何价格都请马上重新设置代理的费率！</el-row>
          <el-row style="color:red" v-if="headName=='TONGYONG_HEAD'">注：通用套餐是否次支持月生效、是否支持连续充值、是否支持分月到账请参考上游套餐，以上游套餐为准！</el-row>
        </el-form-item>
      </el-form>
    </el-card>
  </el-container>
</template>

<script>
import {packageconserve, getSetMeal} from '@/api/getData'

export default {
  name: "addMeal",
  data() {
    return {
      id: '',
      headId: '',
      headName: '',
      headType: '',
      rules: {
        mealType: [
          {required: true, message: '请选择套餐类型', trigger: 'change'}
        ],
        mealName: [
          {required: true, message: '请输入套餐名称', trigger: 'blur'},
          {min: 3, max: 30, message: '长度在 3 到 30 个字符', trigger: 'blur'}
        ],
        terOfValidity: [
          {required: true, message: '请输入有效期', trigger: 'blur'},
          {validator: this.eqValidata, min: 1, max: 36, trigger: 'blur'},
        ],
        terOfValidity2: [
          {required: true, message: '请输入有效期', trigger: 'blur'},
          {validator: this.eqValidata, min: 7, max: 720, trigger: 'blur'}
        ],
        terOfValidity4: [
          {required: true, message: '请输入有效期', trigger: 'blur'},
          {validator: this.eqValidata, min: 7, max: 360, trigger: 'blur'}
        ],
        terOfValidity3: [
          {required: true, message: '请输入有效期', trigger: 'blur'},
          {validator: this.eqValidata, trigger: 'blur'}
        ],
        mealSpec: [
          {required: true, message: '请输入流量', trigger: 'blur'},
          {validator: this.flowValidata, min: 1, max: 9999999, trigger: 'blur'}
        ],
        mealSpec6: [
          {required: true, message: '请输入解封流量阈值', trigger: 'blur'},
          {validator: this.flowValidata, min: 1, max: 9999999, trigger: 'blur'}
        ],
        officialPrice: [
          {required: true, message: '请输入官方售价', trigger: 'blur'},
          {validator: this.numberValidata, min: 0.1, max: 5000, trigger: 'blur'}
        ],
        ceilingPrice: [
          {required: true, message: '请输入代理售价上限', trigger: 'blur'},
          {validator: this.numberValidata, min: 0.1, max: 5000, trigger: 'blur'}
        ],
        oemCostPrice: [
          {required: true, message: '请输入代OEM成本价', trigger: 'blur'},
          {validator: this.numberValidata, min: 0.1, max: 5000, trigger: 'blur'}
        ],
        seq: [
          {required: true, message: '请输入排序序号', trigger: 'blur'},
          {validator: this.seqValidate, min: 1, max: 5000, trigger: 'blur'}
        ],
        dictMonthlyPayment: [//是否到月分账
          {required: true, message: '请选择是否分月到账', trigger: 'blur'},
        ],
        dictDayPayment: [//是否分期到账
          {required: true, message: '请选择是否分月到账', trigger: 'blur'},
        ],
        dictDayNumber: [//是否分期到账--是 必填周期天数
          {required: true, message: '请填写分期到账周期', trigger: 'blur'},
          {validator: this.eqValidata, min: 1, max: 24, trigger: 'blur'}
        ],
        dictIsTerOfValiditySuper: [//是否连续充值
          {required: true, message: '请选择是否连续充值', trigger: 'blur'},
        ],
        cardFlowImaginary: [
          {required: true, message: '请输入权限值', trigger: 'blur'},
          {pattern: /^(\-|\+)?\d+(\.\d{1,2})?$/, message: '请填写正确的权限值,保留两位小数'}
        ],
        thirdId: [
          {required: true, message: '请输入上游编号', trigger: 'blur'},
        ],
        limitMb: [
          {required: true, message: '请输入限速阈值', trigger: 'blur'},
          {validator: this.numberValidata, min: 1, max: 9999999, trigger: 'blur'},
          {validator: this.checkLimitmb, trigger: 'blur'}
        ],
        limitSpeed: [
          {required: true, message: '请选择限速等级', trigger: 'blur'},
        ],

      },
      form: {
        dictIsPreStorage: 'NO',//是否次月生效
        dictMonthlyPayment: 'NO', //是否分月到账
        dictDayPayment: 'NO', //是否分期到账
        dictDayNumber: '',  //分期到账每期天数
        dictIsTerOfValiditySuper: 'C_NO',//是否连续充值
        mealType: '1',
        terOfValidity: '',
        terOfValidity2: '',
        terOfValidity3: '',
        terOfValidity4: '',
        ceilingPrice: "",
        dictMealType: "",//套餐类型
        dictSetMealGroupType: "",//套餐大类
        headId: "",
        id: "",
        mealName: "",
        mealSpec: "",
        mealSpec6: "",
        mealSpecUnit: 'MB',
        officialPrice: "",
        dictTerOfValidity: 'MONTH',
        seq: "",
        cardFlowImaginary: 1,
        thirdId: null,
        oemCostPrice: "",
        dictOperator: 'CHINAMOBILE',
        speeds: [{
          limitSpeed: '',
          limitMb: '',
        }],
      },
      pagekey: '',
      radioList: [],
      loading: false,
      disabled: false,
      showFormItemDay: false,       //控制表单项是否显示：有基础走基础，无基础走加油 =>>> 有效期（日）、
      showFormItemMonth: false,     //控制表单项是否显示：有基础走基础，无基础走加油 =>>> 有效期（月）、是否分月到账、是否次月生效、是否连续充值
      showFormItemSpeedList: false, //控制表单项是否显示：限速类型、限速条件
    }
  },
  async created() {
    console.log('走了这个')
    this.headId = this.$route.query.headId ? this.$route.query.headId : '';
    if (!this.headId) {
      this.$router.push({path: '/road/roadList'})
    }
    //修改
    this.id = this.$route.query.id ? this.$route.query.id : '';
    if (this.id) {
      this.disabled = true;//编辑状态下不可以修改套餐类型
      this.initData();
    }
    this.headName = this.$route.query.headName;
    this.form.dictSetMealGroupType = this.$route.query.headName;
    this.form.dictMealType = 'SETMEAL_BASE';
    this.form.mealType = '1';
    this.showFormItemDay = false;       //控制表单项是否显示：有基础走基础，无基础走加油 =>>> 有效期（日）、
    this.showFormItemMonth = false;     //控制表单项是否显示：有基础走基础，无基础走加油 =>>> 有效期（月）、是否分月到账、是否次月生效、是否连续充值
    this.showFormItemSpeedList = false; //控制表单项是否显示：限速类型、限速条件
    //月套餐：是否分月到账、是否次月生效、是否连续充值显示条件：有基础走基础，无基础走加油

    if (this.headName.indexOf('MONTH') > 0 || this.headName === 'LIMIT_SPEED') {
      this.showFormItemDay = false;
      this.showFormItemMonth = true;
    } else if (this.headName.indexOf('DAY') > 0 && this.headName != 'LIMIT_SPEED_DAY_PLUS') {
      this.showFormItemMonth = false;
      this.showFormItemDay = true;
    }

    if (this.headName === 'BASE_MONTH' || this.headName === 'BASE_DAY') {  //基础套餐
      this.radioList = [
        {value: '1', label: '基础套餐'},
        {value: '2', label: '加油包'},
      ];
    }

    if (this.headName === 'LIMIT_SPEED_DAY' || this.headName === 'LIMIT_SPEED' || this.headName === "LIMIT_SPEED_DAY_PLUS") { //解封包
      this.radioList = [
        {value: '1', label: '基础套餐'},
        {value: '2', label: '加油包'},
        {value: '3', label: '解封包'},
      ];
      this.showFormItemSpeedList = true;
    }
    if (this.headName === "SUPERPOSITION_MONTH" || this.headName === "SUPERPOSITION_DAY") {  //加油包
      this.radioList = [
        {value: '2', label: '加油包'}
      ];
      this.form.dictMealType = 'SETMEAL_ADD_OIL';
      this.form.mealType = '2';
    }
    if (this.headName === 'TONGYONG_HEAD') {  //通用套餐
      this.radioList = [
        {value: '1', label: '基础套餐'},
        {value: '2', label: '加油包'},
        {value: '4', label: '通用套餐'},
      ];
      this.form.dictMealType = 'TONGYONG_SETMEAL';
      this.showFormItemDay = false;
      this.showFormItemMonth = false;
      this.showFormItemSpeedList = false;
    }
    this.pagekey = await this.$getPageKey();
  },
  methods: {
    async initData() {
      let s = await getSetMeal({id: this.id});
      if (s._result) {
        this.form.dictMealType = s.data.dictMealType;//套餐类型
        if (this.form.dictMealType === 'SETMEAL_BASE' || this.form.dictMealType === 'TONGYONG_SETMEAL') {
          this.form.mealType = '1'
        } else if (this.form.dictMealType === 'SETMEAL_ADD_OIL') {
          this.form.mealType = '2'
        } else if (this.form.dictMealType === 'RELIEVE_LIMIT') {
          this.form.mealType = '3'
        } else if (this.form.dictMealType === 'TONGYONG_HEAD') {
          this.form.mealType = '4'
        }
        this.form.id = s.data.id;
        this.form.mealName = s.data.mealName;
        this.form.terOfValidity = s.data.terOfValidity + '';    //有效期(月)
        let mealSpec = s.data.mealSpec;
        if (mealSpec >= 1024) {
          this.form.mealSpecUnit = 'GB';                      //有效期(月/日)
          mealSpec = parseFloat(mealSpec / 1024).toFixed(2);
        } else {
          this.form.mealSpecUnit = 'MB';
        }
        this.form.mealSpec = mealSpec;                        //流量
        this.form.officialPrice = s.data.officialPrice;       //官方售价(元)
        this.form.ceilingPrice = s.data.ceilingPrice;         //代理售价上限(元)
        this.form.oemCostPrice = s.data.oemCostPrice;         //OEM成本价
        this.form.cardFlowImaginary = s.data.cardFlowImaginary;//权限值
        this.form.seq = s.data.seq;                           //排序
        this.form.dictOperator = s.data.dictOperator;         //达量限速卡类型
        this.form.thirdId = s.data.thirdId;                   //上游套餐编号
        this.form.dictIsPreStorage = s.data.dictIsPreStorage ? s.data.dictIsPreStorage : 'NO';                              //是否次月生效
        this.form.dictMonthlyPayment = s.data.dictMonthlyPayment ? s.data.dictMonthlyPayment : 'NO';                        //是否分月到账
        this.form.dictIsTerOfValiditySuper = s.data.dictIsTerOfValiditySuper ? s.data.dictIsTerOfValiditySuper : 'C_NO';    //是否连续充值
        this.form.dictDayNumber = "";                           //分期数
        if (this.form.dictMealType === 'RELIEVE_LIMIT') {
          this.form.mealSpec6 = mealSpec;
        }
        //有效期(天)// if(((this.headName==='BASE_DAY'||this.headName==='LIMIT_SPEED_DAY')&&this.form.mealType==='1')||this.headName==='SUPERPOSITION_DAY'){
        if (this.showFormItemDay) {
          this.form.terOfValidity2 = s.data.terOfValidity + '';
          this.form.dictTerOfValidity = 'DAY';
        }
        if (this.headName === 'TONGYONG_HEAD') {
          this.form.terOfValidity3 = s.data.terOfValidity + '';
          this.form.dictTerOfValidity = s.data.dictTerOfValidity;
        }
        if(this.headName=="LIMIT_SPEED_DAY_PLUS"){
          this.form.terOfValidity4 = s.data.terOfValidity + '';
          this.form.dictTerOfValidity = s.data.dictTerOfValidity;
          this.form.dictMonthlyPayment = "NO";
          this.form.dictDayPayment = s.data.dictMonthlyPayment ? s.data.dictMonthlyPayment : 'NO';                        //是否分期到账
          this.form.dictDayNumber = s.data.cycle?s.data.cycle:'';
        }
        let arr = JSON.parse(s.data.speeds);
        if (arr && arr.length > 0) {
          arr.forEach((item, index) => {
            if (item.limitMb >= 1024 || this.form.mealSpecUnit === 'GB') {
              item.limitMb = parseFloat(item.limitMb / 1024).toFixed(2);
            }
            this.form.speeds[index].limitMb = item.limitMb;
            this.form.speeds[index].limitSpeed = item.limitSpeed;
            if (index !== arr.length - 1) {
              this.addspeed()
            }
          });
        }
        this.changeMealType(this.form.mealType)
      }
    },
    async onSubmit() {
      let _this = this;

      if (this.form.officialPrice * 1 < this.form.oemCostPrice * 1) {
        this.$message.error("官方售价不能低于OEM成本价！");
        return;
      }
      if (this.form.ceilingPrice * 1 < this.form.oemCostPrice * 1) {
        this.$message.error("代理设置的上限价格不能低于OEM成本价！");
        return;
      }
      if (this.form.ceilingPrice * 1 < this.form.officialPrice * 1) {
        this.$message.error("代理设置的上限价格不能低于官方售价！");
        return;
      }
      this.loading = true;
      console.log("test ")
      this.$refs['form'].validate((valid) => {
        console.log(valid)
        if (valid) {
          let cardFlowImaginary = this.headName !== 'TONGYONG_HEAD' ? this.form.cardFlowImaginary : 0;//权限值

          // if(!((this.headName==='LIMIT_SPEED_DAY'||this.headName==='LIMIT_SPEED')&&(this.form.mealType==='2'||this.form.mealType==='3'))&&this.headName!=='TONGYONG_HEAD'){
          //   cardFlowImaginary = this.form.cardFlowImaginary;  //权限
          // }
          // if(this.headName==='SUPERPOSITION_MONTH'||this.headName==='TONGYONG_HEAD'){
          //
          // }
          // if((this.headName==='BASE_MONTH'&&this.form.mealType==='1')||this.headName==='SUPERPOSITION_MONTH'||this.headName==='LIMIT_SPEED'){
          //   terOfValidity = this.form.terOfValidity;
          //   dictTerOfValidity = 'MONTH'
          // }
          //
          // if(((this.headName==='BASE_DAY'||this.headName==='LIMIT_SPEED_DAY')&&this.form.mealType==='1')||this.headName==='SUPERPOSITION_DAY'){
          //   terOfValidity = this.form.terOfValidity2;
          //   dictTerOfValidity = 'DAY'
          // }
          // if(this.headName==='TONGYONG_HEAD'){
          //   terOfValidity = this.form.terOfValidity3;
          //   dictTerOfValidity = this.form.dictTerOfValidity
          // }
          // if((this.headName==='BASE_MONTH'||this.headName==='LIMIT_SPEED')&&this.form.dictMealType === 'SETMEAL_BASE'){
          //   dictMonthlyPayment = this.form.dictMonthlyPayment
          // }
          // let thirdId = this.headName==='TONGYONG_HEAD'?this.form.thirdId:'';//上游套餐编号
          let thirdId = this.form.thirdId;//上游套餐编号(不管是否是通用套餐，只要有值就回传)
          // if(_this.headName==='TONGYONG_HEAD'){
          //   thirdId = _this.form.thirdId
          // }
          let dictIsPreStorage = 'NO';            //是否次月生效
          let dictMonthlyPayment = 'NO';          //是否分月到账
          let dictIsTerOfValiditySuper = 'C_NO';  //是否连续充值
          let cycle = '';                         //高级达量限速套餐（日）使用的周期数
          if (this.showFormItemMonth || this.headName === 'TONGYONG_HEAD') {
            dictIsPreStorage = this.form.dictIsPreStorage;
            dictMonthlyPayment = this.form.dictMonthlyPayment;
            dictIsTerOfValiditySuper = this.form.dictIsTerOfValiditySuper;
          }
          if (this.headName === "LIMIT_SPEED_DAY_PLUS" ) {
            dictIsPreStorage = this.form.mealType=='1'?this.form.dictIsPreStorage:'NO';
            dictMonthlyPayment = this.form.mealType=='1'?this.form.dictDayPayment:'NO';
            dictIsTerOfValiditySuper = this.form.mealType=='1'?this.form.dictIsTerOfValiditySuper:'C_NO';
            cycle = this.form.mealType=='1'?this.form.dictDayNumber:'';
          }
          let terOfValidity = '';                 //有效期时间
          let dictTerOfValidity = '';             //有效期单位 月 or 日
          if (this.headName === 'TONGYONG_HEAD') {
            terOfValidity = this.form.terOfValidity3;
            dictTerOfValidity = this.form.dictTerOfValidity
          } else if (this.showFormItemMonth) {
            terOfValidity = this.form.terOfValidity;
            dictTerOfValidity = 'MONTH'
          } else if (this.showFormItemDay) {
            terOfValidity = this.form.terOfValidity2;
            dictTerOfValidity = 'DAY'
          }
          else if(this.headName === 'LIMIT_SPEED_DAY_PLUS'){
            terOfValidity = this.form.mealType=='1'?this.form.terOfValidity4:'';
            dictTerOfValidity = 'DAY'
          }



          // if(_this.headName!=='BASE_DAY'&&_this.headName!=='SUPERPOSITION_DAY'&&_this.headName!=='LIMIT_SPEED_DAY'){
          //   dictIsTerOfValiditySuper
          // }
          let dictOperator = '';            //达量限速卡类型
          let speeds = [];                  //限速条件
          let mealSpec = this.form.mealSpec;//流量值
          if (this.showFormItemSpeedList) {
            dictOperator = this.form.dictOperator;
            if (_this.form.speeds) {
              for (let item of _this.form.speeds) {
                let obj = {};
                obj.limitMb = item.limitMb;
                if (_this.form.mealSpecUnit === 'GB') {
                  obj.limitMb = item.limitMb * 1024;
                }
                obj.limitSpeed = item.limitSpeed;
                speeds.push(obj);
              }
            }
          }
          if (this.form.mealType === '3') {
            mealSpec = this.form.mealSpec6
          }
          mealSpec = this.form.mealSpecUnit === 'GB' ? mealSpec * 1024 : mealSpec;
          // if(_this.form.mealType==='1'&&(_this.headName==='LIMIT_SPEED_DAY'||_this.headName==='LIMIT_SPEED')){
          //
          //
          // }
          //流量
          // if(_this.form.dictMealType==='RELIEVE_LIMIT'&&(_this.headName==='LIMIT_SPEED_DAY'||_this.headName==='LIMIT_SPEED')){
          //   mealSpec = _this.form.mealSpec6;
          // }
          // if(_this.form.mealSpecUnit==='GB'){
          //   mealSpec = mealSpec * 1024;
          // }


          let form = {
            dictMealType: this.form.dictMealType,				            //套餐类型
            dictSetMealGroupType: this.form.dictSetMealGroupType,		//套餐大类
            headId: this.headId,								                    //套餐大类id
            id: this.id,									                          //套餐id
            mealName: this.form.mealName,								            //套餐名称
            mealSpec,         								                      //流量
            officialPrice: this.form.officialPrice,						      //官方售价(元)
            ceilingPrice: this.form.ceilingPrice,							      //代理售价上限(元)
            oemCostPrice: this.form.oemCostPrice,							      //OEM成本价
            seq: this.form.seq,									                    //排序
            cardFlowImaginary,                  						        //权限值 / 虚值
            dictIsPreStorage,                                       //是否次月生效
            terOfValidity,						                              //有效期
            dictTerOfValidity,                  				            //有效期类型 月 / 日
            dictMonthlyPayment,                                     //是否分月到账
            speeds: JSON.stringify(speeds),                         //限速条件
            dictOperator,                                           //运营商
            dictIsTerOfValiditySuper,                               //连续充值
            thirdId,                                                 //上游套餐编号
            cycle                                                   //周期数（高级达量限速套餐日专用套餐）
          };

          packageconserve(form, {pagekey: this.pagekey});
          this.$message.success("套餐初始化中，请稍后查看");
          this.loading = false;
          setTimeout(() => {
            this.$router.back(-1);
          }, 1000);

        } else {
          this.loading = false;
          return false;
        }
      });
    },
    cancel() {
      this.$router.back(-1)
    },

    checkLimitmb(rule, value, callback) {
      if (this.form.mealSpec * 1 - value * 1 < 0) {
        callback(new Error('请不要超过总流量'))
      } else {
        callback()
      }
    },
    removespeed(item) {
      var index = this.form.speeds.indexOf(item)
      if (index !== -1) {
        this.form.speeds.splice(index, 1)
      }
    },
    addspeed() {
      this.form.speeds.push({
        limitSpeed: '',
        limitMb: '',
      });
    },
    changeMealType(val) {
      this.form.mealType = val;
      if (val === '1') {
        this.form.dictMealType = 'SETMEAL_BASE';
        if (this.headName === 'TONGYONG_HEAD') {
          this.form.dictMealType = 'TONGYONG_SETMEAL';
        }

      } else if (val === '2') {
        this.form.dictMealType = 'SETMEAL_ADD_OIL';
      } else if (val === '3') {
        this.form.dictMealType = 'RELIEVE_LIMIT';
      } else if (val === '4') {
        this.form.dictMealType = 'TONGYONG_HEAD';
      }
      this.showFormItemDay = false;
      this.showFormItemMonth = false;
      this.showFormItemSpeedList = false;

      if (((this.headName === 'BASE_DAY' || this.headName === 'LIMIT_SPEED_DAY') && this.form.mealType === '1') || (this.headName === 'SUPERPOSITION_DAY' && this.form.mealType === '2')) {//以日为单位的套餐大类
        this.showFormItemDay = true;
        this.showFormItemMonth = false;
      }
      if (((this.headName === 'BASE_MONTH' || this.headName === 'LIMIT_SPEED') && this.form.mealType === '1') || (this.headName === 'SUPERPOSITION_MONTH' && this.form.mealType === '2')) {//以月为单位的套餐大类
        this.showFormItemDay = false;
        this.showFormItemMonth = true;
      }
      //达量限速套餐（月）、
      if (this.headName === 'LIMIT_SPEED' && this.form.mealType === '1') {
        this.showFormItemMonth = true;
        this.showFormItemDay = false;
        this.showFormItemSpeedList = true;
      }
      //达量限速套餐（日）
      if (this.headName === 'LIMIT_SPEED_DAY' && this.form.mealType === '1') {
        this.showFormItemDay = true;
        this.showFormItemMonth = false;
        this.showFormItemSpeedList = true;
      }
      //高级达量限速套餐（日）
      if(this.headName === 'LIMIT_SPEED_DAY_PLUS' && this.form.mealType === '1'){
        this.showFormItemDay = true;
        this.showFormItemMonth = false;
        this.showFormItemSpeedList = true;
      }
    },
    //切换是否分月到账 此条件与连续充值互斥
    changeDictMonthlyPayment(val) {
      this.form.dictMonthlyPayment = val;
      if (val == 'YES') {
        this.form.dictIsPreStorage = 'NO'
      }
    },
    //切换是否分期到账 此条件与连续充值互斥
    changeDictDayPayment(val) {
      this.form.dictDayPayment = val;
      if (val == 'YES') {
        this.form.dictIsPreStorage = 'NO';
        this.form.dictDayNumber = '';
      }
    },

    //切换是否连续充值 此条件与分月到账互斥
    changeDictIsPreStorage(val) {
      this.form.dictIsPreStorage = val;
      if (this.form.dictIsPreStorage == 'YES') {
        this.form.dictMonthlyPayment = 'NO'
      }
    },
    changCardFact() {
      for (let item of this.form.speeds) {
        item.limitSpeed = ''
      }
    },
    /*****************以下方法为表单校验*********************/
    numberValidata(rule, value, callback) {
      let reg = /^[0-9]+(\.[0-9]{1,2})?$/;
      if (!value || !reg.test(value) || value < rule.min || value > rule.max) {
        return callback(new Error('您修改您输入的金额，最小值' + rule.min + '，最大值' + rule.max));
      } else {
        callback()
      }
    },
    flowValidata(rule, value, callback) {
      let reg = /^[0-9]+(\.[0-9]{1,2})?$/;
      if (!value || !reg.test(value) || value < rule.min || value > rule.max) {
        return callback(new Error('您修改您输入的流量值，最小值' + rule.min + '，最大值' + rule.max));
      } else {
        callback()
      }
    },
    eqValidata(rule, value, callback) {
      if (!value) {
        return callback(new Error('请输入有效期'));
      }
      let reg = /^\d{1,3}$/;
      if (rule && rule.min) {
        if (reg.test(value)) {
          if (value < rule.min || value > rule.max) {
            return callback(new Error('您修改您输入的有效期，最小值' + rule.min + '，最大值' + rule.max));
          } else {
            callback()
          }
        } else {
          return callback(new Error('有效期格式错误'));
        }
      } else {
        if (this.form.dictTerOfValidity === 'DAY') {
          if (value < 7 || value > 720) {
            return callback(new Error('您修改您输入的有效期，最小值7，最大值720'));
          } else {
            callback()
          }
        } else if (this.form.dictTerOfValidity === 'MONTH') {
          if (value < 1 || value > 36) {
            return callback(new Error('您修改您输入的有效期，最小值1，最大值36'));
          } else {
            callback()
          }
        }
      }
    },
    seqValidate(rule, value, callback) {
      let reg = /^[1-9]\d*$/;
      if (!reg.test(value)) {
        callback(new Error("请输入正整数"))
      } else {
        callback()
      }
    },
  }
}
</script>

<style scoped>
.cardBlock {
  display: flex;
  justify-content: center;
  align-items: center;
  margin: 50px auto;
  padding: 20px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);
  width: 1000px
}

</style>
<style>
.el-card__body {
  width: 100%;
}
</style>
